<?php

	session_start();

	// Fichero principal
	include "../../../include/init.php";	
	include "../class/epcms.class.php";	
	
?>
   	  
	  <style type="text/css">
	  
	  	.image {
			float:left;margin-top:10px;margin-left:10px;
		}
		
		.image .fotito {
			width:150px;height:100px;border: #000000 solid 1px;
		}
		
		.image .fotito:hover {
			border:#FF0000 solid 1px;
			cursor:pointer;
		}
	  
		/* paginacion */
		.paginacion {
			padding: 3px;
			margin: 3px;
			text-align: center;
		}
		
		.paginacion a {
			padding: 2px 5px 2px 5px;
			margin: 2px;
			border: 1px solid #93b84f;
			text-decoration: none;
			color: #991427;
		}
		
		.paginacion a:hover, .paginacion a:active {
			border: 1px solid #893600;
			color: #FFFFFF;
			background-color: #893600;
		}
		
		.paginacion .actual {
			padding: 2px 5px 2px 5px;
			margin: 2px;
			border: 1px solid #893600;
			font-weight: bold;
			background-color: #893600;
			color: #FFFFFF;
		}
      </style>
      
      
      <link href="<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/uploadify/uploadify.css" type="text/css" rel="stylesheet" />      
        
      <script type="text/javascript" src="<?php echo _CONFIG_INSTALL_DIR_; ?>/js/jquery-1.5.1.min.js"></script>
      <script type="text/javascript" src="<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/uploadify/swfobject.js"></script>
      <script type="text/javascript" src="<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
      <script type="text/javascript">

      $(document).ready(function() {

        $('#file_upload').uploadify({   
          'uploader'  : '<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/uploadify/uploadify.swf',  
          'script'    : '<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/uploadify/uploadify.php?id_type=<?php echo $_REQUEST['type_image']; ?>',  
          'cancelImg' : '<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/uploadify/cancel.png',  
          'folder'    : '<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/imgcache/tmp',  
		  //'folder'    : '/4dsystems/imgdata/images',  
          'displayData': 'percentage',
		  'fileExt'     : '*.jpg;*.gif;*.png',
		  'fileDesc'    : 'Web Image Files (.JPG, .GIF, .PNG)',
		  'removeCompleted' : true,
		  'auto'      : true,
		  'method'		: 'post',
		  'buttonText' : 'Upload Image',
		  'multi'	  : false,

			'onComplete' : function(event, ID, fileObj, response, data) {

			  //alert('fileObj: ' + fileObj + ' Response: '+ response + ' data: ' + data);
			  
			  //$('.mostrar_imagen').html('<img src="'+ response +'" />');
			  
			  //parent.$('mostrar_ruta').html('hola');
			  //$.fn.TestFunction("sdfsdfsfsfsd"); 
			  
			  parent.meterImagen(response); 
			  
			  parent.$.fn.colorbox.close(); 

			}		  
        });  
      });
      </script>

<?php
	/*var_dump($_REQUEST);
	var_dump($_GET);
	var_dump($_POST);*/
	// Location
	echo "<b>Image repository</b>";
	
	echo "<div class=\"imgmanager\" style=\"width:100%; height: 500px;\">";
		
		echo "<div class=\"dvLeft\">";
		echo "	<b>Upload files:</b><br>";
		echo "	<input id=\"file_upload\" name=\"file_upload\" type=\"file\" />";
		echo "</div>";	
		
		$epcms = new epcms_base();
		
		//$imagenes = $epcms->getImagesRepositoryByType($_REQUEST['type_image']);
		
		$pagina = (int) $_GET['pag']; // parametro de pagina
		
		$numero_registros_pagina = 10;
                            
		if (!$pagina) { 
                                                        
			$inicio = 0; 
			$pagina = 1; 
                                                                            
        } else { 
                                                                            
           $inicio = ($pagina - 1) * $numero_registros_pagina; 
        }		
		
		$num_total_registros = count($epcms->getImagesRepositoryByType($_REQUEST['type_image']));
        $total_paginas = ceil($num_total_registros / $numero_registros_pagina);										
                                                        
        $imagenes = $epcms->getImagesRepositoryPaginationByType($_REQUEST['type_image'],$inicio, $numero_registros_pagina);
		 
		 		
		
		foreach($imagenes as $imagen) {
			echo "<div class=\"image\"><img class=\"fotito\" style=\"\" src=\"../../../../imgdata/images/".$imagen['physical_name']."\" title=\"".$imagen['id']."\" onclick=\"parent.meterImagen(".$imagen['id'].");parent.$.fn.colorbox.close();\" /><br><a href=\"#\" title=\"Delete image ".$imagen['original_name']."\" onclick=\"DeleteImage(".$imagen['id'].")\"><img src=\"../../../img/b_borrar.gif\" alt=\"Delete image ".$imagen['original_name']."\" /></a>&nbsp;&nbsp;".$imagen['original_name']."</div>";
		}
		
		if ($total_paginas > 1){ 
			echo "<div style=\"clear:both\"></div><p class=\"paginacion\">";
			
			if($pagina > 1)
				echo "<a href=\"?type_image=".$_REQUEST['type_image']."&pag=".($pagina-1)."\"><< Previous</a>&nbsp;&nbsp;";
					for ($i = 1; $i <= $total_paginas; $i++){ 
						if ($pagina == $i) 
							echo "&nbsp;<strong class=\"actual\">".$pagina."</strong>&nbsp;"; 
						else 
							echo "&nbsp;<a href=\"?type_image=".$_REQUEST['type_image']."&pag=".$i."\">".$i."</a>&nbsp;"; 
					}
									
					if($pagina < $total_paginas)
						echo "&nbsp;&nbsp;<a href=\"?type_image=".$_REQUEST['type_image']."&pag=".($pagina+1)."\">Next >></a>";
			
			echo "</p>"; 
												
		}		
		
	echo "</div>";	
	
?>	
<script type="text/javascript">

	function DeleteImage(id) {
		
		if(confirm('Remove this picture?')) {
		
			$.ajax({
					  type: 'POST',
					  url : '<?php echo _CONFIG_INSTALL_DIR_; ?>/app/epcms/ajax/js_show_image.php',
					  data: 'accion=delete_image&id='+id+'',
					  success : function (data) {
						  //alert(data);
						  if(data) { // si todo va bien (sin cruce, se elimina)
							  location.reload(); // al eliminar, refrescar página
						  } else {
							  alert('You can not delete this image because it is associated with a category or article'); // si está cruzado, escupe alert!
						  }
						  
					  },
					  error : function (data) { 								
						  alert('Error de llamada AJAX: '+data);
					  }					
				  });
		
		}
	}

</script>